package cn.scw.project.goods.controller;

import cn.scw.common.constant.SysConsts;
import cn.scw.framework.aspectj.lang.annotation.Log;
import cn.scw.framework.aspectj.lang.enums.BusinessType;
import cn.scw.framework.web.controller.BaseController;
import cn.scw.framework.web.domain.AjaxResult;
import cn.scw.framework.web.page.TableDataInfo;
import cn.scw.project.goods.domain.netework.bean.GCommodityOrderAddReq;
import cn.scw.project.goods.domain.netework.bean.GCommodityOrderListReq;
import cn.scw.project.goods.domain.netework.bean.GCommodityOrderUpdReq;
import cn.scw.project.goods.domain.vo.GCommodityOrderVo;
import cn.scw.project.goods.service.IGCommodityOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.jdom.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 商品订单Controller
 *
 * @author api
 * @date 2020-05-20
 */
@Api(tags = "商品订单")
@RestController
@RequestMapping(SysConsts.CONTEXT_PATH+"/goods/commodity/order")
public class GCommodityOrderController extends BaseController {
    @Autowired
    private IGCommodityOrderService gCommodityOrderService;


    @ApiOperation("查询商品订单列表")
    //@PreAuthorize("@ss.hasPermi('goods:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(GCommodityOrderListReq req) {
        startPage();
        List<GCommodityOrderVo> list = gCommodityOrderService.selectGCommodityOrderList(req);
        return getDataTable(list);
    }

    @ApiOperation("查询我的商品订单列表")
    @GetMapping("/user/list/{statusCode}")
    public TableDataInfo userList(@PathVariable @Param("订单状态（待付款，待发货，待收货，已完成，售后/退换）") String statusCode) {
        startPage();
        List<GCommodityOrderVo> list = gCommodityOrderService.selectGCommodityOrderUserList(statusCode);
        return getDataTable(list);
    }

    @ApiOperation("获取商品订单详细信息")
    //@PreAuthorize("@ss.hasPermi('goods:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(gCommodityOrderService.selectGCommodityOrderVoById(id));
    }

    @ApiOperation("新增商品订单")
    //@PreAuthorize("@ss.hasPermi('goods:order:add')")
    @Log(title = "商品订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GCommodityOrderAddReq req) throws JDOMException, IOException {
        return gCommodityOrderService.insertGCommodityOrder(req);
    }

    @ApiOperation("余额支付")
    @PostMapping("/pay")
    public AjaxResult pay(@RequestParam @ApiParam("订单id") Long id, @RequestParam @ApiParam("密码") String payPassword) {
        return toAjax(gCommodityOrderService.pay(id, payPassword));
    }

    @ApiOperation("修改商品订单")
    //@PreAuthorize("@ss.hasPermi('goods:order:edit')")
    @Log(title = "商品订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(GCommodityOrderUpdReq req) {
        return toAjax(gCommodityOrderService.updateGCommodityOrder(req));
    }

    @ApiOperation("删除商品订单")
    //@PreAuthorize("@ss.hasPermi('goods:order:remove')")
    @Log(title = "商品订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(gCommodityOrderService.deleteGCommodityOrderByIds(ids));
    }
}
